/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockDialogFragment;

/**
 * A dialog fragment that allows user to select a time.
 *
 */
public class MyTimePickerDialogFragment extends SherlockDialogFragment{
	public static final String HOUR_OF_DAY_KEY="hourOfDay";
	public static final String MINTUES_KEY="minutes";
	public static final String IS_24_HOUR_KEY="is24Hour";
	public static final String TAG="timePickerDialog";
	private OnTimeSetListener _listener;
	private int _hour,_min;
	private boolean _is_24hours;
	
	public MyTimePickerDialogFragment(OnTimeSetListener listener) {
		_listener=listener;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args=getArguments();
		if (args!=null) {
			_hour=args.getInt(HOUR_OF_DAY_KEY,0);
			_min=args.getInt(MINTUES_KEY,0);
			_is_24hours=args.getBoolean(IS_24_HOUR_KEY,false);
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return new TimePickerDialog(getSherlockActivity(), _listener, _hour,_min,_is_24hours); 
	}
	
	public MyTimePickerDialogFragment(){
		
	}
	
	public static MyTimePickerDialogFragment newInstance(){
		return new MyTimePickerDialogFragment(null);
	}
	
	/**
	 * The recommended way of creating a new reference by taking the pre-defined
	 * hour, minutes, and a callback listener.
	 * 
	 * @param hourOfDay
	 * @param minutes
	 * @param is24Hour
	 * @param listener
	 * @return
	 */
	public static MyTimePickerDialogFragment newInstance(int hourOfDay, int minutes,boolean is24Hour, OnTimeSetListener listener) {
		Bundle b=new Bundle();
		b.putBoolean(IS_24_HOUR_KEY, is24Hour);
		b.putInt(HOUR_OF_DAY_KEY, hourOfDay);
		b.putInt(MINTUES_KEY, minutes);
		
		MyTimePickerDialogFragment fragment=new MyTimePickerDialogFragment(listener);
		fragment.setArguments(b);
		return fragment;
	}
	
	public void set_listener(OnTimeSetListener _listener) {
		this._listener = _listener;
	}
	
	public OnTimeSetListener get_listener() {
		return _listener;
	}
	
	@Override
	public void onActivityCreated(Bundle arg0) {
		super.onActivityCreated(arg0);
		getDialog().dismiss();
	}
	
}